<template>
  <div class="login-page">
    <div class="back">
      <!--
      .native修饰符
      在自定义组件上绑定原生事件, 比如: @click 默认是不支持的
      但是如果加了.native修饰符, 则支持
      原理: 就是把@click事件穿透到子组件的根标签上
      绑定组件的原生事件，属于组件根元素
      -->
      <!-- $router.back() 返回上一次访问路由，forward go -->
      <geek-icon @click.native="$router.back()" name="esay-close"></geek-icon>
    </div>
    <h3 class="title">短信登录</h3>
    <!-- 表单 -->
     <van-form ref="form">
      <van-field name="mobile" v-model="form.mobile" placeholder="请输入手机号" :rules="rules.mobile"></van-field>
      <van-field v-model="form.code" placeholder="请输入验证码" :rules="rules.code">
        <template #button>
          <span class="send" @click="send()">
            {{ second === 0 ? '发送验证码' : `${second}秒后发送` }}
          </span>
        </template>
      </van-field>
    </van-form>
    <van-button  @click="onSubmit">登录</van-button>
  </div>
</template>

<script>
import { sendMessageApi, userLoginApi } from '@/apis/index'
import { Toast } from 'vant'

export default {
  name: 'LoginPage',
  data () {
    return {
      form: {
        mobile: '',
        code: ''
      },
      rules: {
        mobile: [
          { required: true, message: '请输入手机号' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不对' }
        ],
        code: [
          { required: true, message: '请输入验证码' },
          { pattern: /^\d{6}$/, message: '验证码是6个数字' }
        ]
      },
      // 倒计时秒数
      second: 0,
      // 定时器ID
      timer: null
    }
  },
  methods: {
    async onSubmit () {
      try {
        await this.$refs.form.validate()
        const [, res] = await userLoginApi(this.form)
        console.log(res)
        // 存储token
        this.$store.commit('user/setToken', res.data.data.token)
        // 提示用户
        Toast.success('登录成功')
        // 返回登录前的页面
        // vue2的页面中使用路由: this.$router / this.$route
        // vue3: const router = useRouter()  / const route = useRoute()
        this.$router.replace(this.$route.query.returnUrl || '/')
      } catch (error) {
        console.log(error)
      }
    },
    async send () {
      // 已发送，不做事
      if (this.second > 0) return
      // 校验
      await this.$refs.form.validate('mobile')
      // 发短信
      const [err] = await sendMessageApi(this.form.mobile)
      // 失败
      if (err) return this.$toast.fail('发送失败')
      // 成功 倒计时
      this.second = 60
      if (this.timer) clearInterval(this.timer)
      this.timer = setInterval(() => {
        this.second--
        if (this.second <= 0) clearInterval(this.timer)
      }, 1000)
      // 发送成功后，直接将将验证码写入表单中  因为接口不会返回验证码，只返回是否成功的状态，所以手动将约定好的验证码写入
      this.form.code = '246810'
    }
  }
}
</script>

<style scoped lang="less">
.login-page {
  padding: 0 32px;

  .back {
    height: 60px;
    display: flex;
    align-items: center;

    .geek-icon {
      font-size: 20px;
      color: #ccc;
      position: relative;
      left: -15px;
    }
  }

  .title {
    font-size: 22px;
    line-height: 1;
    padding: 30px 0;
  }

  .van-cell {
    padding: 20px 0;

    &::after {
      left: 0;
      right: 0;
    }
  }

  .van-button {
    width: 100%;
    margin-top: 40px;
    height: 50px;
    color: #fff;
    font-size: 16px;
    border: none;
    background: linear-gradient(to right, #fe4f4e, #fc6528);
  }
}
.send {
  font-size: 12px;
  color: #A5A6AB;
}
</style>
